package controller;

import javax.swing.event.TableModelEvent;
import manager.BookManager;
import view.BookDetailView;
import java.awt.event.*;

import javax.swing.JList;

import entity.Book;
import entity.User;
import entity.state.book.BookState;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 * @author s759487
 * @version 1.0
 * @created 11-Apr-2011 15:52:59
 */
public class BookController {

    private view.BookListView listView;
    private view.BookDetailView detailView;
    private BookManager bookManager = BookManager.getInstance();

    public MouseListener lsl = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            String classname = e.getComponent().getClass().getSimpleName();
            if (classname.startsWith("JList")) {
                Object o = e.getSource();
                JList j = (JList) o;
                Book b = bookManager.findById(j.getSelectedIndex());
                detailView = new BookDetailView(b, tml);
            }
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
    };
    TableModelListener tml = new TableModelListener() {

        public void tableChanged(TableModelEvent e) {
            Book book;
            TableModel tm = (TableModel) e.getSource();
            book = (Book) bookManager.findById(new Integer((String) tm.getValueAt(0, 0)));
            BookState state = book.getState();
            User value;
            switch (e.getColumn()) {
                case 1:
                    book.setAuthor((String) tm.getValueAt(0, 1));
                    break;
                case 2:
                    book.setTitle((String) tm.getValueAt(0, 2));
                    break;
                case 3:
                    value = (User) tm.getValueAt(0, 3);
                    if (value.equals("")) {
                        try {
                            state.returnBook(book);
                        } catch (Exception ex) {
                        }
                    } else {
                        try {
                            state.lendBook(book, value);
                        } catch (Exception ex) {
                        }
                    }
                    break;
                case 4:
                    value = (User) tm.getValueAt(0, 4);
                    if (value.equals("")) {
                        try {
                            state.cancelReservation(book);
                        } catch (Exception ex) {
                        }
                    } else {
                        try {
                            state.reserve(book, value);
                        } catch (Exception ex) {
                        }
                    }
                    break;
            }
        }
    };
}
